<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <title>websocket</title>

    <script type="text/javascript">
        function offLine() {
        ws.close();
    }

    var ws = new WebSocket("ws://localhost:8081");
        function WebSocketTest()
        {
            if ("WebSocket" in window)
            {
                alert("您的浏览器支持 WebSocket!");

                // 打开一个 web socket
                ws.onopen = function()
                {

                };

                ws.onmessage = function (evt)
                {
                    var received_msg = evt.data;
                    console.log("接受数据"+received_msg)
                    ws.send("客户端发送数据")
                };

                ws.onclose = function()
                {
                    // 关闭 websocket
                    console.log("连接已关闭...");
                };
            }

            else
            {
                // 浏览器不支持 WebSocket
                alert("您的浏览器不支持 WebSocket!");
            }
        }

        function sendMessage() {
            ws.send(document.getElementById('send').value);
        }

    </script>

</head>
<body>

<div id="sse">
    <a href="javascript:WebSocketTest()">开始监听</a>
    <label>
        输入：
        <input id="send"/>
    </label>
    <button class="btnSend" onclick="sendMessage()">发送</button>
    <button class="btnSend" onclick="offLine()">下线</button>
</div>

</body>
</html>